﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.CallTypes.Authorization;
using LeadThing.AbpZeroTemplate.CallTypes.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.CallTypes
{
    /// <summary>
    /// 一键报警类型表服务实现
    /// </summary>
    


    public class CallTypeAppService : AbpZeroTemplateAppServiceBase, ICallTypeAppService
    {
        private readonly IRepository<CallType, long> _callTypeRepository;
        private readonly ICallTypeListExcelExporter _callTypeListExcelExporter;


        private readonly CallTypeManage _callTypeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public CallTypeAppService(IRepository<CallType, long> callTypeRepository,
CallTypeManage callTypeManage
      , ICallTypeListExcelExporter callTypeListExcelExporter
  )
        {
            _callTypeRepository = callTypeRepository;
            _callTypeManage = callTypeManage;
            _callTypeListExcelExporter = callTypeListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<CallType> _callTypeRepositoryAsNoTrack => _callTypeRepository.GetAll().AsNoTracking();


        /// <summary>
        /// 获取所有一键报警类型
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<CallTypeListDto>> GetAllCallTypeList()
        {
            var query = _callTypeRepositoryAsNoTrack;
            var items = await query.ToListAsync();
            var callTypeListDto = items.MapTo<List<CallTypeListDto>>();

            return new ListResultDto<CallTypeListDto>(callTypeListDto);
        }


        #endregion


        #region 一键报警类型表管理

        /// <summary>
        /// 根据查询条件获取一键报警类型表分页列表
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType)]
        public async Task<PagedResultDto<CallTypeListDto>> GetPagedCallTypesAsync(GetCallTypeInput input)
        {

            var query = _callTypeRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var callTypeCount = await query.CountAsync();

            var callTypes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var callTypeListDtos = callTypes.MapTo<List<CallTypeListDto>>();
            return new PagedResultDto<CallTypeListDto>(
            callTypeCount,
            callTypeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取一键报警类型表信息进行编辑或修改 
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType)]
        public async Task<GetCallTypeForEditOutput> GetCallTypeForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetCallTypeForEditOutput();

            CallTypeEditDto callTypeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _callTypeRepository.GetAsync(input.Id.Value);
                callTypeEditDto = entity.MapTo<CallTypeEditDto>();
            }
            else
            {
                callTypeEditDto = new CallTypeEditDto();
            }

            output.CallType = callTypeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取一键报警类型表ListDto信息
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType)]
        public async Task<CallTypeListDto> GetCallTypeByIdAsync(EntityDto<long> input)
        {
            var entity = await _callTypeRepository.GetAsync(input.Id);

            return entity.MapTo<CallTypeListDto>();
        }







        /// <summary>
        /// 新增或更改一键报警类型表
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType)]
        public async Task CreateOrUpdateCallTypeAsync(CreateOrUpdateCallTypeInput input)
        {
            if (input.CallTypeEditDto.Id.HasValue)
            {
                await UpdateCallTypeAsync(input.CallTypeEditDto);
            }
            else
            {
                await CreateCallTypeAsync(input.CallTypeEditDto);
            }
        }

        /// <summary>
        /// 新增一键报警类型表
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType_CreateCallType)]
        public virtual async Task<CallTypeEditDto> CreateCallTypeAsync(CallTypeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<CallType>();

            entity = await _callTypeRepository.InsertAsync(entity);
            return entity.MapTo<CallTypeEditDto>();
        }

        /// <summary>
        /// 编辑一键报警类型表
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType_EditCallType)]
        public virtual async Task UpdateCallTypeAsync(CallTypeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _callTypeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _callTypeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除一键报警类型表
        /// </summary>
        //[AbpAuthorize(CallTypeAppPermissions.CallType_DeleteCallType)]
        public async Task DeleteCallTypeAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _callTypeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除一键报警类型表
        /// </summary>
        [AbpAuthorize(CallTypeAppPermissions.CallType_DeleteCallType)]
        public async Task BatchDeleteCallTypeAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _callTypeRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 一键报警类型表的Excel导出功能

        [AbpAuthorize(CallTypeAppPermissions.CallType)]
        public async Task<FileDto> GetCallTypeToExcel()
        {
            var entities = await _callTypeRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<CallTypeListDto>>();

            var fileDto = _callTypeListExcelExporter.ExportCallTypeToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
